package com.rth.wx.Handler;

import com.rth.wx.enums.WechatMsgTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component
public class WechatMsgHandlerFactory implements InitializingBean {

    @Resource
    private List<WechatMsgHandler> wechatMsgHandlerList;
    Map<WechatMsgTypeEnum, WechatMsgHandler> handlerMap = new HashMap<>();

    public WechatMsgHandler getWechatMsgHandler(String msgType) {
        WechatMsgHandler wechatMsgHandler = handlerMap.get(WechatMsgTypeEnum.getMsgTypeEnum(msgType));
        return wechatMsgHandler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        wechatMsgHandlerList.forEach(
                wechatMsgHandler -> {
                    handlerMap.put(wechatMsgHandler.getMsgType(), wechatMsgHandler);
                }
        );


    }
}
